﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraGauges.Win.Gauges.State;
using Zero.POCO.Donnees;
using Zero.POCO.Views;

namespace DevExpress.MailClient.Win.Controls {
    public partial class WidgetStructureAudienceMoyen : UserControl {
        Timer updateTimer = new Timer();
        static Color positiveColor = Color.FromArgb(70, 158, 165);
        static Color negativeColor = Color.FromArgb(165, 70, 113);
        public WidgetStructureAudienceMoyen()
        {
            InitializeComponent();
            updateTimer.Interval = 1000;
            updateTimer.Tick += OnTimerTick;
            updateTimer.Start();
            OnTimerTick(this, EventArgs.Empty);
        }
    
        int index = 0;
       // StockData previousData;
        void OnTimerTick(object sender, EventArgs e) {
          /*  if(IsDisposed) { updateTimer.Stop(); return; }
            StockData data = StockDataCreator.GetData(index);
            if(index > 0) {
                decimal d = data.ClosePrice - previousData.ClosePrice;
                decimal dq = (d * 100) / data.ClosePrice;
                labelControl5.Text = Math.Round( d, 2, MidpointRounding.ToEven).ToString().TrimEnd('0');
                stateIndicatorComponent1.StateIndex =  previousData.ClosePrice - data.ClosePrice < 0 ? 0 : 1;
                if(stateIndicatorComponent1.StateIndex == 0) {
                    labelControl5.ForeColor = positiveColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=70,158,165>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
                else {
                    labelControl5.ForeColor = negativeColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=165,70,113>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
            }
            previousData = data;
            index++;*/
        }
        private void OnCustomDrawElement(object sender, DevExpress.XtraGauges.Core.Primitive.CustomDrawElementEventArgs e) {
            e.Handled = true;
            StateIndicatorComponent indicator = sender as StateIndicatorComponent;
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;
            Point p3 = Point.Empty;
            int height = (int)(e.Info.BoundBox.Width * Math.Sqrt(3) / 2);
            p1.X = (int)(e.Info.BoundBox.Width / 2);
            p2.X = (int)e.Info.BoundBox.Left;
            p3.X = (int)e.Info.BoundBox.Right;
            int y = (int)((e.Info.BoundBox.Height - height) / 2);
            Color stateColor = Color.Empty;
            if(indicator.StateIndex == 0) {
                p1.Y = y;
                p2.Y = p3.Y = y +height;
                stateColor = positiveColor;
            }
            else {
                p1.Y = y + height;
                p2.Y = p3.Y = y;
                stateColor = negativeColor;
            }
            e.Context.Graphics.FillPolygon(new SolidBrush(stateColor), new Point[] { p1, p2, p3 });
        }

        public void UpdateStat(StructureVague structureAudiences)
        {
            ccStructureMoyenne.Series.Clear();
            var series1 = new Series("Global", ViewType.Line);
            var listRepere = new List<int>();
            listRepere.AddRange(structureAudiences.structureAudiencesVaguePrincpale.ProfilSupportGlobal.ListePenetration.Select(s => s.Order).Distinct());

            foreach (var i in listRepere)
            {
                var valeur = structureAudiences.structureAudiencesVaguePrincpale.ProfilSupportGlobal.ListePenetration.Where(e => e.Order == i).Sum(e => e.Penetration) /7;
                series1.Points.Add(new SeriesPoint(i, valeur));
            }
            
            ccStructureMoyenne.Series.Add(series1);



            var series2 = new Series(structureAudiences.structureAudiencesVaguePrincpale.ProfilSupportPricncipale.SupportView.Nom, ViewType.Line);
            listRepere = new List<int>();
                 listRepere.AddRange(structureAudiences.structureAudiencesVaguePrincpale.ProfilSupportPricncipale.ListePenetration.Select(s => s.Order).Distinct());

            foreach (var i in listRepere)
            {
                var valeur = structureAudiences.structureAudiencesVaguePrincpale.ProfilSupportPricncipale.ListePenetration.Where(e => e.Order == i).Sum(e => e.Penetration) /7;
                series2.Points.Add(new SeriesPoint(i, valeur));
            }

            ccStructureMoyenne.Series.Add(series2);

            foreach (var support in structureAudiences.structureAudiencesVaguePrincpale.ProfilSupport)
            {
                var series3 = new Series(support.SupportView.Nom, ViewType.Line);
                listRepere = new List<int>();
                listRepere.AddRange(support.ListePenetration.Select(s => s.Order).Distinct());

                foreach (var i in listRepere)
                {
                    var valeur = support.ListePenetration.Where(e => e.Order == i).Sum(e => e.Penetration)/7;
                    series3.Points.Add(new SeriesPoint(i, valeur));
                }

                ccStructureMoyenne.Series.Add(series3);
            }
        }
    }
}
